<?php 

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Cache;
use think\Db;
use think\Session;
class Common extends Controller
{
	public function  _initialize()
	{
		if(!Request::instance()->isMobile()){
           exit("Access Denied");
       }
   //     $ip = $this->getIP();
   //     $url = 'http://ip.ws.126.net/ipquery?ip='.$ip;

			// $list= file_get_contents($url);

			//  $list = iconv("gbk","UTF-8",$list);
			// if(strpos($list, '北京')>0){
			// 	header('Location:http://www.baidu.com');
			// 	exit("Access Denied");
			// }
	}

	public function getIP() {
		if (getenv('HTTP_CLIENT_IP')) { 
		$ip = getenv('HTTP_CLIENT_IP'); 
		} 
		elseif (getenv('HTTP_X_FORWARDED_FOR')) {
		$ip = getenv('HTTP_X_FORWARDED_FOR'); 
		} 
		elseif (getenv('HTTP_X_FORWARDED')) { 
		$ip = getenv('HTTP_X_FORWARDED'); 
		} 
		elseif (getenv('HTTP_FORWARDED_FOR')) { 
		$ip = getenv('HTTP_FORWARDED_FOR'); 

		} 
		elseif (getenv('HTTP_FORWARDED')) { 
		$ip = getenv('HTTP_FORWARDED'); 
		} 
		else { 
		$ip = $_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}
	public function lists($key){
		$cache = 'redis';
		if($_SERVER['HTTP_HOST']=='osman.cn'){
			$cache = 'file';
		}
        if(!$data = cache::store($cache)->get('zb_list')){
            $data = Db::name('zb')->where('display',1)->order('paixu desc')->column('id,title,desc,pic,number,index,template,subtitle','name');
            cache::store($cache)->set('zb_list',$data,'600');
        }
        if(!empty($key)){
          unset($data[$key]);
        }
        return $data;
    }

    public function get_current($key){
		$cache = 'redis';
		if($_SERVER['HTTP_HOST']=='osman.cn'){
			$cache = 'file';
		}
        if(!$data = cache::store($cache)->get('zb_list')){
            $data = Db::name('zb')->where('display',1)->order('paixu desc')->column('id,title,desc,pic,number,index,template,subtitle','name');
            cache::store($cache)->set('zb_list',$data,'600');
        }
        if(!empty($key)){
          return $data[$key];
        }
        return false;
    }

	//获取第三方授权码
	public function curl_post($data, $url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
	}
}
